package com.hd.sup.modules.vehicle.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.hd.common.utils.CollectionUtils;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.Pair;
import com.hd.common.utils.ValueUtils;
import com.hd.sup.modules.alarm.service.IAlarmDailyStatisService;
import com.hd.sup.modules.alarm.service.IAlarmDetailService;
import com.hd.sup.modules.alarm.util.AlarmUtils;
import com.hd.sup.modules.alarm.util.TemplateUtils;
import com.hd.sup.modules.poi.model.VehicleAlarmExport;
import com.hd.sup.modules.vehicle.mapper.VehicleAlarmDailyStatisMapper;
import com.hd.sup.modules.vehicle.model.VehicleAlarmDailyStatis;
import com.hd.sup.modules.vehicle.service.IVehicleAlarmDailyStatisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chentl123
 * @since 2020-12-28
 */
@Service
public class VehicleAlarmDailyStatisServiceImpl extends ServiceImpl<VehicleAlarmDailyStatisMapper, VehicleAlarmDailyStatis> implements IVehicleAlarmDailyStatisService {

    @Resource
    private IAlarmDailyStatisService alarmDailyStatisService;

    @Resource
    private IAlarmDetailService alarmDetailService;

    @Override
    public List<Map<String, Object>> getAlarmVehiclePage(Page<Map<String, Object>> page, VehicleAlarmDailyStatis vehicleAlarmDailyStatis) {
        return this.baseMapper.getAlarmVehiclePage(page, vehicleAlarmDailyStatis);
    }

    @Override
    public List<VehicleAlarmExport> getAlarmVehicleExportData(Page<VehicleAlarmDailyStatis> page, VehicleAlarmDailyStatis vehicleAlarmDailyStatis) {
        return this.baseMapper.getAlarmVehicleExportData(page, vehicleAlarmDailyStatis);
    }

    @Override
    public List<Map<String, Object>> getVehicleAlarmTrendData(VehicleAlarmDailyStatis vehicleAlarmDailyStatis, String alarmType, List<String> deptIds) {
        return this.baseMapper.getVehicleAlarmTrendData(vehicleAlarmDailyStatis, alarmType, deptIds);
    }

    @Override
    public List<VehicleAlarmDailyStatis> getTodayStatisData(String currentDate) {
        return this.baseMapper.getTodayStatisData(currentDate);
    }

    @Override
    public void refreshVehicleAlarmDailyStatis(VehicleAlarmDailyStatis vehicleAlarmDailyStatis) {
        this.baseMapper.refreshVehicleAlarmDailyStatis(vehicleAlarmDailyStatis);
    }

    @Override
    public Map<String, Object> getAlarmTypeDistribution(VehicleAlarmDailyStatis param, String timeFlag, boolean pre) {
        String trans_type = param.getTransType();
        Map<String, Object> alarm_type_distribution = Maps.newHashMap();
        Map<String, Object> alarm_data = Maps.newHashMap();
        Map<String, String> templates = Maps.newHashMap();

        Map<String, Object> result = alarmDetailService.getAlarmSumDistribution(param);
        if (CollectionUtils.isNotEmpty(result)) {
            Integer as = ValueUtils.parseInteger(result.get("alarmSum"));
            Integer nss = ValueUtils.parseInteger(result.get("night_stop_sum"));
            Integer ogs = ValueUtils.parseInteger(result.get("off_gps_sum"));
            Integer ols = ValueUtils.parseInteger(result.get("off_line_sum"));
            Integer fs = ValueUtils.parseInteger(result.get("fatigue_sum"));
            Integer osn = ValueUtils.parseInteger(result.get("over_speed_num"));


            String nssRate = as == 0 ? "0" : ValueUtils.getRate(nss, as, 2);
            String ogsRate = as == 0 ? "0" : ValueUtils.getRate(ogs, as, 2);
            String olsRate = as == 0 ? "0" : ValueUtils.getRate(ols, as, 2);
            String fsRate = as == 0 ? "0" : ValueUtils.getRate(fs, as, 2);
            String osnRate = as == 0 ? "0" : ValueUtils.getRate(osn, as, 2);

            //2 生成报警数量总结模板
            String template_alarm_num = TemplateUtils.getAlarmStatisDesc(TemplateUtils.getParagraphs(param.getParagraphs()), timeFlag, trans_type, as, fs, fsRate, nss, nssRate, ogs, ogsRate, ols, olsRate, osn, osnRate);
            templates.put("t1", template_alarm_num);

            alarm_data.put("alarmSum", as);
            alarm_data.put("night_stop_sum", nss);
            alarm_data.put("off_gps_sum", ogs);
            alarm_data.put("over_speed_sum", osn);
            alarm_data.put("off_line_sum", ols);
            alarm_data.put("fatigue_sum", fs);
        } else {
            //补零
            alarm_data.put("alarmSum", 0);
            alarm_data.put("night_stop_sum", 0);
            alarm_data.put("off_gps_sum", 0);
            alarm_data.put("over_speed_sum", 0);
            alarm_data.put("off_line_sum", 0);
            alarm_data.put("fatigue_sum", 0);

            //2 生成报警数量总结模板
            String template_alarm_num = TemplateUtils.getAlarmStatisDesc("一", timeFlag, trans_type, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
            templates.put("t1", template_alarm_num);
        }


        //4 pre 为false 表示本次查询不是昨日数据。 此时需要查询趋势
        if (!pre) {
            List<Map<String, Object>> trendData;
            Pair<String, String> timeRange = DateUtil.getRecent6Range(param.getDateStr(), timeFlag);
            List<String> dateScope = DateUtil.getRecent6DateScope(param.getDateStr(), timeFlag);


            param.setStartDateStr(timeRange.first);
            param.setEndDateStr(timeRange.second);
            trendData = alarmDailyStatisService.trendDataForReport(param);

            //1 生成时间范围报警趋势数据
            // 如果是月报 或 年报 则需要查询 当前月往前推6个月 或 当前周往前推6周的  或 本年度往前推 6 年的 报警趋势数据 | 无数据则补零
            LinkedHashMap<String, Object> finalTrendData = AlarmUtils.groupTrendData(trendData, timeFlag, dateScope);
            if (MapUtil.isNotEmpty(finalTrendData)) alarm_type_distribution.put("trend", finalTrendData);

            //2 生成报警趋势总结模板
            String template_alarm_trend = TemplateUtils.getAlarmTrendDesc(finalTrendData, timeFlag);
            templates.put("t2", template_alarm_trend);
        }

        alarm_type_distribution.put("data", alarm_data);
        alarm_type_distribution.put("templates", templates);

        return alarm_type_distribution;
    }

    @Override
    public Map<String, Object> generateAlarmReportData(VehicleAlarmDailyStatis param, String timeFlag, boolean pre) {
        Map<String, Object> alarm = Maps.newHashMap();

        Pair<String, String> datePair = AlarmUtils.getStartEndDate(param.getDateStr(), timeFlag, pre);
        param.setStartDateStr(datePair.first);
        param.setEndDateStr(datePair.second);


        //1 计算 不同报警类型数量 和 比率
        Map<String, Object> alarm_distribution = getAlarmTypeDistribution(param, timeFlag, pre);
        alarm.put("alarm_distribution", alarm_distribution);

        //2 企业车辆报警比
        Map<String, Object> enterprise_ratio = enterpriseVehicleAlarmRatioReport(param, timeFlag, pre);
        alarm.put("enterprise_ratio_data", enterprise_ratio);

        //3 单车报警数
        Map<String, Object> vehicle_ratio = vehicleAlarmRatioReport(param, timeFlag, pre);
        alarm.put("vehicle_ratio_data", vehicle_ratio);

        return alarm;
    }


    @Override
    public List<Map<String, Object>> getEnterpriseAlarmRatioTop10(VehicleAlarmDailyStatis param) {
        return this.baseMapper.getEnterpriseAlarmRatioTop10(param);
    }

    @Override
    public Map<String, Object> enterpriseVehicleAlarmRatioReport(VehicleAlarmDailyStatis param, String timeFlag, boolean pre) {
        Map<String, Object> res = Maps.newHashMap();
        Map<String, String> templates = Maps.newHashMap();

        Pair<String, String> datePair = AlarmUtils.getStartEndDate(param.getDateStr(), timeFlag, pre);
        param.setStartDateStr(datePair.first);
        param.setEndDateStr(datePair.second);


        //page2
        // 1 计算 企业单车报警比
        List<Map<String, Object>> result = getEnterpriseAlarmRatioTop10(param);
        // 2 生成企业车辆报警比 模板描述
        String enterpriseAlarmRatioDesc1 = TemplateUtils.getEnterpriseAlarmRatioDesc1(timeFlag);
        templates.put("t1", enterpriseAlarmRatioDesc1);

        if (CollectionUtils.isNotEmpty(result)) {
            String enterpriseName = ValueUtils.parseString(result.get(0).get("enterprise_name"));
            String enterpriseAlarmRatioDesc2 = TemplateUtils.getEnterpriseAlarmRatioDesc2(timeFlag, enterpriseName);
            templates.put("t2", enterpriseAlarmRatioDesc2);

        }

        //构造返回数据
        res.put("templates", templates);
        res.put("data", result);
        return res;
    }

    @Override
    public Map<String, Object> vehicleAlarmRatioReport(VehicleAlarmDailyStatis param, String timeFlag, boolean pre) {
        Map<String, Object> res = Maps.newHashMap();
        Map<String, String> templates = Maps.newHashMap();

        Pair<String, String> datePair = AlarmUtils.getStartEndDate(param.getDateStr(), timeFlag, pre);
        param.setStartDateStr(datePair.first);
        param.setEndDateStr(datePair.second);


        //page2
        // 1 计算 企业单车报警比
        List<Map<String, Object>> result = getVehicleAlarmRatioTop10(param);

        // 2 生成企业车辆报警比 模板描述
        String vehicleAlarmRatioDesc1 = TemplateUtils.getVehicleAlarmRatioDesc1(timeFlag);
        templates.put("t1", vehicleAlarmRatioDesc1);
        if (CollectionUtils.isNotEmpty(result)) {
            String enterpriseName = ValueUtils.parseString(result.get(0).get("enterprise_name"));
            String plat_no = ValueUtils.parseString(result.get(0).get("plat_no"));

            String vehicleAlarmRatioDesc2 = TemplateUtils.getVehicleAlarmRatioDesc2(timeFlag, enterpriseName, plat_no);


            templates.put("t2", vehicleAlarmRatioDesc2);

        }

        //构造返回数据
        res.put("templates", templates);
        res.put("data", result);
        return res;
    }

    @Override
    public List<Map<String, Object>> getVehicleAlarmRatioTop10(VehicleAlarmDailyStatis param) {
        return this.baseMapper.getVehicleAlarmRatioTop10(param);
    }
}
